/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mam.rbac.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.web.QueryFilter;
import com.foreveross.bsl.common.utils.web.QueryFilter.Operator;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.mam.model.rbac.domain.Role;
import com.foreveross.bsl.mam.rbac.application.PrivilegeApplication;
import com.foreveross.bsl.mam.rbac.application.RbacUserRoleApplication;
import com.foreveross.bsl.mam.rbac.application.RoleApplication;
import com.foreveross.bsl.mam.rbac.application.vo.RoleVO;
import com.foreveross.bsl.mongodb.domain.repository.EntityRepository;
import com.foreveross.bsl.push.application.TagSummaryService;

/**
 * <BR>
 * [功能详细描述]
 * 
 * @author Amberlo
 * @version [mam-application-impl , 2013-6-23]
 */
@Named("roleApplication")
public class RoleApplicationImpl implements RoleApplication {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Inject
	RbacUserRoleApplication rbacApplication;

	@Inject
	PrivilegeApplication privilegeApplication;

	@Inject
	private TagSummaryService defaultTagSummaryService;


	@Override
	public RoleVO saveEntity(RoleVO entity) {
		Role Role = new Role();
		RoleVO existRole = getRoleByRoleName(entity.getRolename());
		if (existRole != null)
			return null; // 若已存在
		BeanHelper.copyProperties(Role, entity);
		Role.save();
		entity.setId(Role.getId());
		return entity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(RoleVO entity) {
		Role role = new Role();
		BeanHelper.copyProperties(role, entity);
		role.delete();
		defaultTagSummaryService.removeTagForAllApps("privileges",
				role.getName());
		rbacApplication.deleteByRoleId(role.getId());
		privilegeApplication.deletePrivilegesByRoleID(role.getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public RoleVO getEntity(String id) {
		Role Role = new Role();
		Role = (Role) Role.findOne(id);
		RoleVO vo = new RoleVO();
		BeanHelper.copyProperties(vo, Role);
		return vo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<RoleVO> findAll() {
		Role Role = new Role();
		Iterable iterable = Role.findAll();
		if (iterable != null) {
			List<RoleVO> result = new ArrayList<RoleVO>();
			RoleVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				Role obj = (Role) iterator.next();

				vo = new RoleVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable
	 * (org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<RoleVO> findByPageable(Pageable pageable) {
		Role databaseInfo = new Role();
		Page page = databaseInfo.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<RoleVO> result = new ArrayList<RoleVO>();
			RoleVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				Role obj = (Role) iterator.next();

				vo = new RoleVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<RoleVO> pageVO = new PageImpl<RoleVO>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	@Override
	public RoleVO updateEntity(RoleVO entity) {
		RoleVO oldRole = this.getEntity(entity.getId());
		Role databaseInfo = new Role();
		BeanHelper.copyProperties(databaseInfo, entity);
		if (!entity.getName().equals(oldRole.getName())) {
			defaultTagSummaryService.removeTagForAllApps("privileges",
					oldRole.getName());
		}
		databaseInfo.save();
		BeanHelper.copyProperties(entity, databaseInfo);
		return null;
	}

	@Override
	public RoleVO getRoleByRoleName(String roleName) {
		Role role = new Role();
		List<Role> rs = role.findListByProperty("rolename", OperatEnum.EQ,
				roleName);
		if (rs == null)
			return null;
		if (rs.size() == 0)
			return null;
		role = rs.get(0);
		RoleVO roleVO = new RoleVO();
		BeanHelper.copyProperties(roleVO, role);
		return roleVO;
	}

	@Override
	public RoleVO getRoleByName(String name) {
		Role role = new Role();
		List<Role> rs = role.findListByProperty("name", OperatEnum.EQ, name);
		if (rs == null)
			return null;
		if (rs.size() == 0)
			return null;
		role = rs.get(0);
		RoleVO roleVO = new RoleVO();
		BeanHelper.copyProperties(roleVO, role);
		return roleVO;
	}

	@Override
	public Page<RoleVO> findRolesBySpec(int pageNumber, int pageSize,
			QuerySpecs querySpecs) {
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize);
		if (querySpecs == null) {
			querySpecs = new QuerySpecs();
		}
		QueryFilter filter = new QueryFilter("roleType", Operator.EQ, "1");// 用户自定义角色
		querySpecs.addFilter(filter);
		Role role = new Role();
		Page<Role> page = role.findByQuerySpecs(querySpecs, pageable);
		return convertToRoleVO(page, pageable);
	}

	private Page<RoleVO> convertToRoleVO(Page<Role> page, Pageable pageable) {
		// Page<RoleVO> result = new
		// PageImpl<RoleVO>(BeanMapper.mapList(page.getContent(), RoleVO.class),
		// pageable, page.getTotalElements());
		List<RoleVO> result = new ArrayList<RoleVO>();
		RoleVO vo;
		for (Iterator iterator = page.getContent().iterator(); iterator
				.hasNext();) {
			Role obj = (Role) iterator.next();

			vo = new RoleVO();
			BeanHelper.copyProperties(vo, obj);
			result.add(vo);
		}
		Page<RoleVO> pageVO = new PageImpl<RoleVO>(result, pageable,
				page.getTotalElements());
		return pageVO;
	}
}
